package com.wzy.controller;

import com.wzy.annotation.LimitRequest;
import com.wzy.annotation.SystemLog;
import com.wzy.domain.OssFile;
import com.wzy.domain.ResponseResult;
import com.wzy.domain.bo.ArticleBO;
import com.wzy.domain.bo.ArticleExamineBO;
import com.wzy.domain.entity.Article;
import com.wzy.domain.entity.ArticleRecord;
import com.wzy.domain.vo.*;
import com.wzy.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.wzy.constant.SystemConstants.TRUE;

/**
 * 文章管理控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/article")
public class ArticleController
{
    private final ArticleService articleService;

    /**
     * 查询热门文章
     */
    @GetMapping("/hotArticleList")
    @SystemLog(basenessName = "查询热门文章")
    public ResponseResult<Collection<HotArticleVO>> hotArticleList()
    {
        return articleService.getHotArticleList();
    }

    /**
     * 查询最新发布文章
     */
    @GetMapping("/latestArticleList")
    @SystemLog(basenessName = "查询最新发布文章")
    public ResponseResult<List<ArticleListVO>> latestArticleList()
    {
        return articleService.getLatestArticleList();
    }

    /**
     * 查询文章列表
     */
    @GetMapping("/articleList")
    @SystemLog(basenessName = "查询文章列表")
    public ResponseResult<PageVO> articleList(Integer pageNum, Integer pageSize, Long categoryId)
    {
        return articleService.getArticleListByCategoryId(pageNum, pageSize, categoryId);
    }

    /**
     * 查询文章详情
     */
    @GetMapping(value = "/articleDetail/{id}")
    @SystemLog(basenessName = "查询文章详情")
    public ResponseResult<ArticleVO> article(@PathVariable Long id)
    {
        return articleService.getArticle(id);
    }

    /**
     * 更新文章浏览量
     */
    @PutMapping(value = "/updateViewCount/{id}")
    @SystemLog(basenessName = "更新文章浏览量")
    @LimitRequest(time = 60000)
    public ResponseResult<Void> updateViewCount(@PathVariable Long id)
    {
        return articleService.updateViewCount(id);
    }

    /**
     * 上传文章图片
     */
    @PostMapping("/uploadImage")
    @LimitRequest(time = 24 * 60 * 60 * 1000, count = 10, type = "USER", limitAdmin = false, tip = TRUE)
    public ResponseResult<OssFile> upload(@RequestBody MultipartFile file)
    {
        return articleService.uploadImage(file);
    }

    /**
     * 删除文章图片
     */
    @DeleteMapping("/deleteImage")
    @SystemLog(basenessName = "删除文章图片")
    public ResponseResult<OssFile> deleteImage(String url)
    {
        return articleService.deleteImage(url);
    }

    /**
     * 保存文章
     */
    @PostMapping(value = "/postArticle")
    @SystemLog(basenessName = "保存文章")
    public ResponseResult<Object> postArticle(@Validated @RequestBody SaveArticleVO article)
    {
        return articleService.postArticle(article);
    }

    /**
     * 获取草稿
     */
    @GetMapping("/getDraft")
    @SystemLog(basenessName = "获取草稿")
    public ResponseResult<List<SaveArticleVO>> getDraftList()
    {
        return articleService.getDraftList();
    }

    /**
     * 获取编辑记录
     */
    @GetMapping("/getEditHistory")
    @SystemLog(basenessName = "获取编辑记录")
    public ResponseResult<List<Object>> getEditHistory()
    {
        return articleService.getEditHistory();
    }

    /**
     * 删除草稿
     */
    @DeleteMapping("/deleteDraft/{articleId}")
    @SystemLog(basenessName = "删除草稿")
    public ResponseResult<Void> deleteDraft(@PathVariable Long articleId)
    {
        return articleService.deleteDraft(articleId);
    }

    /**
     * 获取我发布的文章总数
     */
    @GetMapping("/getMyArticleTotal")
    @SystemLog(basenessName = "获取我发布的文章总数")
    public ResponseResult<Map<String, Integer>> getMyArticleTotal()
    {
        return articleService.getMyArticleTotal();
    }

    /**
     * 获取我发布的文章总浏览量
     */
    @GetMapping("/getTotalView")
    @SystemLog(basenessName = "获取我发布的文章总浏览量")
    public ResponseResult<Map<String, Long>> getTotalView()
    {
        return articleService.getTotalView();
    }

    /**
     * 查询提交审核文章
     */
    @GetMapping("/examineArticle")
    @SystemLog(basenessName = "查询提交审核文章")
    public ResponseResult<PageVO> getNotExamineArticle(@RequestBody ArticleExamineBO articleExamineBO)
    {
        return articleService.getNotExamineArticle(articleExamineBO);
    }

    /**
     * 修改审核文章状态
     */
    @PutMapping("/editArticleExamine")
    @SystemLog(basenessName = "修改审核文章状态")
    public ResponseResult<Void> editArticleExamine(@RequestBody Article article)
    {
        return articleService.editArticleExamine(article);
    }

    /**
     * 获取文章编辑详情
     */
    @GetMapping("/getArticleEditRecord")
    @SystemLog(basenessName = "获取文章编辑详情")
    public ResponseResult<ArticleRecord> getArticleEditRecord(Long recordId)
    {
        return articleService.getArticleEditRecord(recordId);
    }

    /**
     * 管理员查询文章
     */
    @GetMapping("/selectPageArticle")
    @SystemLog(basenessName = "管理员查询文章")
    public ResponseResult<PageVO> selectPageArticle(ArticleBO articleBO)
    {
        return articleService.selectPageArticle(articleBO);
    }

}
